﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using Excel_12 = Microsoft.Office.Interop.Excel;
namespace Nhom16_PTTKHTTT_12HCB
{
    class ExportToExcel
    {
        public void Export(DataGridView dt, string strTieude)
        {
            try
            {
                Excel_12.Application excel = new Excel_12.Application();
                excel.Application.Workbooks.Add(true);
                Excel_12.Application oExcel_12 = null;                // Excel_12 Application
                Excel_12.Workbook oBook = null;                       // Excel_12 Workbook
                Excel_12.Sheets oSheetsColl = null;                   // Excel_12 Worksheets collection
                Excel_12.Worksheet oSheet = null;                     // Excel_12 Worksheet
                Excel_12.Range oRange = null;                         // Cell or Range in worksheet
                Object oMissing = System.Reflection.Missing.Value;
                oExcel_12 = new Excel_12.Application();
                // tao moi 1 excel
                oExcel_12.Visible = true;//hien thi chuong trinh excel
                // mo mot excel
                oExcel_12.UserControl = true;
                //System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
                // them vao 1 workbook.
                oBook = oExcel_12.Workbooks.Add(oMissing);
                // Get worksheets collection
                oSheetsColl = oExcel_12.Worksheets;
                oSheet = (Excel_12.Worksheet)oSheetsColl.get_Item("Sheet1");//ghi vao sheet 1
                //ghi ten cua phieu
                oRange = (Excel_12.Range)oSheet.Cells[1, 2];//dong bat dau, cot bat dau
                oRange.Value2 = strTieude;//lay tieu de
                oRange.Columns.AutoFit();

                oRange = (Excel_12.Range)oSheet.Cells[2, 1];//dong 2,ghi o cot 2 
                oRange.Value2 = "Ngày lập";
                oRange.Columns.AutoFit();
                //ghi ngay nhap
                oRange = (Excel_12.Range)oSheet.Cells[2, 2];//dong 2,ghi o cot 2 
                oRange.Value2 = DateTime.Now.ToShortDateString();
                oRange.Columns.AutoFit();
                //ghi dau tien trong datagridview
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    //ghi dong dau tien o dong thu 2 cua excel
                    oRange = (Excel_12.Range)oSheet.Cells[3, j + 1];//d3 c0 ghi masp,d3 c1 ghi ten sp.....                    
                    oRange.Value2 = dt.Columns[j].HeaderText;
                    oRange.Columns.AutoFit();//gian cot
                }
                //ghi noi dung cua datagridview
                for (int i = 0; i < dt.Rows.Count; i++)//duet dong
                {
                    for (int j = 0; j < dt.Columns.Count; j++)//duyet cot
                    {
                        //bat dau ghi tren excel tu dong thu 3 cot thu 1
                        oRange = (Excel_12.Range)oSheet.Cells[i + 4, j + 1];//Cells[i + 2, j + 1]  
                        oRange.Value2 = dt.Rows[i].Cells[j].EditedFormattedValue.ToString();
                        oRange.Columns.AutoFit();
                    }
                }
                ////muc giam
                //oRange = (Excel_12.Range)oSheet.Cells[dataGridViewXHienThi.Rows.Count + 4, 2];//dong bd, cot bd
                //oRange.Value2 = "Mức giảm";

                //oRange = (Excel_12.Range)oSheet.Cells[dataGridViewXHienThi.Rows.Count + 4, 3];//dong bd, cot bd
                //khachhangBlo.Khachhang.MaKH = txtMaKH_TT.Text.Trim().ToString();

                //if (khachhangBlo.KiemTraKHTonTai() == true)
                //    oRange.Value2 = Session_QuiDinh.MucGiam;
                //else
                //    oRange.Value2 = "0";

                //oRange = (Excel_12.Range)oSheet.Cells[dataGridViewXHienThi.Rows.Count + 4, 4];//dong bd, cot bd
                //oRange.Value2 = "%";
                ////tong tien
                //oRange = (Excel_12.Range)oSheet.Cells[dataGridViewXHienThi.Rows.Count + 6, 2];//dong bd, cot bd
                //oRange.Value2 = "Tổng tiền";

                //oRange = (Excel_12.Range)oSheet.Cells[dataGridViewXHienThi.Rows.Count + 6, 3];//dong bd, cot bd
                //oRange.Value2 = txtTongTien.Text;

                //oRange = (Excel_12.Range)oSheet.Cells[dataGridViewXHienThi.Rows.Count + 6, 4];//dong bd, cot bd
                //oRange.Value2 = "VNĐ";
                ////nhan vien xuat
                //oRange = (Excel_12.Range)oSheet.Cells[dataGridViewXHienThi.Rows.Count + 7, 2];//dong bd, cot bd
                //oRange.Value2 = "Người xuất";
                ////nhan vien xuat
                //oRange = (Excel_12.Range)oSheet.Cells[dataGridViewXHienThi.Rows.Count + 8, 2];//dong bd, cot bd
                //oRange.Value2 = Session.ChucVu;
                ////nhan vien xuat
                //oRange = (Excel_12.Range)oSheet.Cells[dataGridViewXHienThi.Rows.Count + 8, 3];//dong bd, cot bd
                //oRange.Value2 = Session.TenNV;
                oRange.Columns.AutoFit();
                excel.Quit();
                oExcel_12.Quit();

            }
            catch (Exception ex)
            {

            }
        }
   }
}
